package com.xia.controller;

import com.xia.pojo.Books;
import com.xia.service.BookServiceImpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //controller调用service层
  @Autowired
    @Qualifier("BookServiceImpl")
    private BookServiceImpl bookService;

    public void setBookService(BookServiceImpl bookService) {
        this.bookService = bookService;
    }
    //查询所有的书籍，并返回到一个书籍的展示页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);

        return "allBook";

    }
    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";

    }
    //增加书籍
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println("addBook=>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";//重定向到@RequestMapping("/toAddBook")请求
    }
    //跳转到修改页面
    @RequestMapping("/toUpdateBook")
    public String toAddUpdate(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("Qbooks",books);

        return "updateBook";

    }
    //修改书籍
    @RequestMapping("/updateBook")
    public String toUpdateBooks(Books books){
        System.out.println(books);
         bookService.updateBook(books);
         System.out.println("修改成功"+books);
        return "redirect:/book/allBook";
    }
    //删除书籍
    @RequestMapping("/toDelectBooks")
    public String toDelect(int  id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";


    }

}
